import BouncingShapes.bounceboxframework.Shape;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

class FileFilterImpl implements FileFilter {
    @Override
    public boolean accept(File pathname) {
        if (pathname.getName().toLowerCase().endsWith(".txt")) {
            return true;
        }
        // 若pathname是文件夹 则返回true 继续遍历这个文件夹
        return pathname.isDirectory();
    }
}

public class ShapeGUI extends JFrame {
    private ShapeService shapeService = new ShapeService();
    private ArrayList<Shape> parsedShapesList;
    private HashMap<String, Integer> countByTypeMap;

    public void windowCenterAlignment(JFrame frame) {
        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int windowx = (int) screensize.getWidth() / 2 - frame.getWidth() / 2;
        int windowy = (int) screensize.getHeight() / 2 - frame.getHeight() / 2;
        frame.setLocation(windowx, windowy);
    }

    public ShapeGUI() {}

    public File fileChooser(JFrame frame) {
        JFileChooser fileChooser = new JFileChooser();
        FileFilter fileFilter = new FileFilterImpl();
        fileChooser.setDialogTitle("Choose a file");
        int returnValue = fileChooser.showOpenDialog(frame);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFile();
        } else {
            return null;
        }
    }

    public void MainGUI() {
        JFrame mainFrame = new JFrame("Shape GUI");
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.setSize(400, 300);
        windowCenterAlignment(mainFrame);
        mainFrame.setLayout(new BorderLayout());

        JLabel headLine = new JLabel("Shape GUI");
        headLine.setFont(new Font("Arial", Font.BOLD, 24));
        headLine.setHorizontalAlignment(SwingConstants.CENTER);
        headLine.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 10));
        mainFrame.add(headLine, BorderLayout.NORTH);
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(0, 2, 20, 20));

        JButton chooseFileButton = new JButton("Choose File");
        chooseFileButton.addActionListener(e -> {
            File selectedFile = fileChooser(mainFrame);
            try {
                parsedShapesList = shapeService.parseFile(selectedFile.getAbsolutePath());
                JOptionPane.showMessageDialog(mainFrame, "File parsed successfully!");
            } catch (FileNotFoundException ex) {
                JOptionPane.showMessageDialog(mainFrame, "File not found: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        JButton countShapesButton = new JButton("Count Shapes");
        countShapesButton.addActionListener(e -> {
            try {
                countByTypeMap = shapeService.countByType(parsedShapesList);
                JOptionPane.showMessageDialog(mainFrame, shapeService.hashmapToString(countByTypeMap));
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(mainFrame, "Error counting shapes: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        JButton displayShapesButton = new JButton("Display Shapes");
        displayShapesButton.addActionListener(e -> {
            try {
                shapeService.displayShapes(parsedShapesList);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(mainFrame, "Error drawing shapes: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        JButton totalAreaButton = new JButton("Total Area");
        totalAreaButton.addActionListener(e -> {
            try {
                double totalArea = shapeService.totalArea(parsedShapesList);
                JOptionPane.showMessageDialog(mainFrame, "Total Area: " + totalArea);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(mainFrame, "Error calculating total area: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            }
        });
        JButton exitButton = new JButton("Exit");
        exitButton.addActionListener(e -> {
            int confirm = JOptionPane.showConfirmDialog(mainFrame, "Are you sure you want to exit?", "Exit", JOptionPane.YES_NO_OPTION);
            if (confirm == JOptionPane.YES_OPTION) {
                System.exit(0);
            }
        });
        mainPanel.add(chooseFileButton);
        mainPanel.add(countShapesButton);
        mainPanel.add(displayShapesButton);
        mainPanel.add(totalAreaButton);
        mainPanel.add(exitButton);
        JPanel wrapperPanel = new JPanel();
        wrapperPanel.setLayout(new BorderLayout());
        wrapperPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        wrapperPanel.add(mainPanel, BorderLayout.CENTER);
        mainFrame.add(wrapperPanel, BorderLayout.CENTER);
        mainFrame.setVisible(true);
    }

    public static void main(String[] args) {
        ShapeGUI shapeGUI = new ShapeGUI();
        shapeGUI.MainGUI();
        //shapeGUI.setVisible(true);
    }
}
